﻿using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using SCM.API.Read.Application.Command.StockCheck;
using SCM.ErrorCode;
using SCM.Extensions.Ledger;
using SCM.Extensions.StockCheck;
using SCM.Infrastructure.Interface;
using System.Text;

namespace SCM.API.Read.Application.Handler.StockCheck
{
    public class GetCheckHandler : IRequestHandler<GetCheckCommand, APIPaging<CheckDTO>>
    {
        IDatabaseContext db;

        public GetCheckHandler(IDatabaseContext db)
        {
            this.db = db;
        }

        public async Task<APIPaging<CheckDTO>> Handle(GetCheckCommand request, CancellationToken cancellationToken)
        {
            var result = new APIPaging<CheckDTO>();

            var countSql = new StringBuilder();
            var sqlBuilder = new StringBuilder();

            sqlBuilder.Append(@"SELECT 
                                        a.id,
                                        a.CheckCode,
                                        a.CheckResult,
                                        d.DepotId,
                                        d.DepotName,
                                        a.CheckType,
                                        a.StartTime,
                                        a.EndTime,
                                        a.OutStatus,
                                        a.EnterStatus,
                                        a.CheckProduct,
                                        a.CheckNum,
                                        a.ProfitNum,
                                        a.isDel
                                FROM CheckModel a  join  DepotModel      d on a.DepotId=d.DepotId
                                    WHERE a.IsDel=0   and d.IsDel=0 ");

            countSql.Append(@"SELECT COUNT(a.id)   FROM CheckModel a  join  DepotModel      d on a.DepotId=d.DepotId
                                    WHERE a.IsDel=0   and d.IsDel=0 ");

            var parameters = new DynamicParameters();

            if (!string.IsNullOrEmpty(request.CheckCode))
            {
                sqlBuilder.Append(" AND CheckCode LIKE @CheckCode ");
                parameters.Add("CheckCode", $"%{request.CheckCode}%");
                countSql.Append(" AND CheckCode LIKE @CheckCode ");
            }
            
            if (request.CheckType != null)
            {
                sqlBuilder.Append("AND CheckType = @CheckType ");
                parameters.Add("CheckType", request.CheckType);
                countSql.Append("AND CheckType = @CheckType ");
            }
           


            sqlBuilder.Append("ORDER BY id DESC ");
            sqlBuilder.Append("LIMIT @PageSize OFFSET @Offset ");
            parameters.Add("PageSize", request.PageSize);
            parameters.Add("Offset", (request.PageIndex - 1) * request.PageSize);

           

            var data = await db.DapperConnection.QueryAsync<CheckDTO>(sqlBuilder.ToString(), parameters);
            var total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);

            result.Msg = "显示成功";
            result.Code = APIEnum.Success;
            result.items = data.ToList();
            result.totalCount = total;
            result.totalPage = (int)Math.Ceiling((double)total / request.PageSize);
            return result;
        }
    }
}
